package com.tianya.common.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.tianya.common.util.Utility;

/*****************************************************************
*Title: 自动完成的三字码输入控件
*Description: 当用户在输入框里面输入前面的字母时，自动提示第一个匹配
*  的结果；当输入全部3个字母时，自动提示城市名称或者提示
*  不存在该三字代码
*Date: 2009.11.09
*Author:Jula (chenwei@from28.cn)  
*Version: 1.0.0.0
*Memo: 本Js提供函数接口：
*Example:(调用示例,如果保存为独立的js文件)
 <script language="javascript" src="Js_ThreeCode.js"></ script>
 <p><input type="text" id="txt1" ctrlType="threecodecity" showError="yes"/></p>
*****************************************************************/
@SuppressWarnings("serial")
public class CitysAction extends BaseAction {

	/**
	 * 
	 */
	@SuppressWarnings("unchecked")
	@Override
	public String actionExecute() throws Exception {
		//页面输出流字符集设定
		response.setContentType("text/xml;charset=utf-8");
		//将JS输出
		PrintWriter pw= this.response.getWriter();
		pw.flush();
		//显示还用层做成
		pw.println("document.write(\"<div id='jjcitycodechenwei' style='font-size:14px;padding:5px; position: absolute; z-index: 9999; width: 200px; min-width: 240px; min-height: 175px; display: none;'></div>\");");
		pw.println("function jjcitycode(){");
		pw.println("	this.citycode = getObjectById(\"jjcitycodechenwei\");	");
		pw.println("	this.eventSrc;");
		pw.println("	this.eventSrcId;");
		pw.println("    this.callBack='';");
		pw.println("	this.leftCol = 'ENAME';");
		pw.println("	this.rightCol = 'NAME';");
		pw.println("	this.selectedStyle = \"jjcity_selected\";");
		pw.println("	this.mainStyle = \"jjcity\";");
		pw.println("	this.lineStyle = \"jjcity_line\";");
		//鼠标经过样式
		pw.println("	this.hotStyle= \"jjcity_hot\";");
		pw.println("	this.pageStyle = \"jjcity_page\";");
		pw.println("	this.selectPageStyle = \"jjcity_pageselect\";");
		//正常样式
		pw.println("	this.normalStyle = \"jjcity_normal\";");
		//事件元素
		pw.println("	this.objExport;");
		//每页显示条数
		pw.println("	this.pagesize = 10;");
		pw.println("	this.matchData = 'matchData';");
		//总页数
		pw.println("	this.pages = [];");
		pw.println("	this.htCityCode={};");
		//初始化建议项目表示
		//pw.println("	this.suggestHTML=\"按拼音检索<hr/>\";");
		pw.println("	this.suggestHTML='';");
		pw.println("	this.pageCount=0;");
		pw.println("	this.currentpage=0;");
		//名称表示
		pw.println("	this.htCityName=[];");
		pw.println("	var tempCityName=[];");
		String str = request.getParameter("city");
		if(!"1".equalsIgnoreCase(str)){
			//机场的情况
			str = "0";
		}
		//数据形式  三子代码|拼音或英文|拼音首字母或英文缩写|中文名称|是否推荐|国内国外区分
//		List<Map> tagCity =  findCitys(str);
		List<Map> tagCity=new ArrayList();
		for(Map m : tagCity){
			pw.println(" tempCityName=[];");
			//数据库ID
			pw.append(" tempCityName[\"ID\"]=\"").append(Utility.nvl(m.get("ID"))).append("\";").println();
			//数据库三字代码
			pw.append(" tempCityName[\"CID\"]=\"").append(Utility.nvl(m.get("CID"))).append("\";").println();
			//城市名字
			pw.append(" tempCityName[\"NAME\"]=\"").append(Utility.nvl(m.get("NAME"))).append("\";").println();
			//拼音或者英文名字
			pw.append(" tempCityName[\"ENAME\"]=\"").append(Utility.nvl(m.get("ENAME"))).append("\";").println();
			//拼音首字母或者英文缩写
			pw.append(" tempCityName[\"SNAME\"]=\"").append(Utility.nvl(m.get("SNAME"))).append("\";").println();
			//是否为建议项目
			pw.append(" tempCityName[\"SUG\"]=\"").append(Utility.nvl(m.get("SUG"))).append("\";").println();
			//国家区分
			pw.append(" tempCityName[\"COU\"]=\"").append(Utility.nvl(m.get("COU"))).append("\";").println();
			pw.append("	this.htCityCode[\"").append(m.get("ID").toString()).append("\"]=tempCityName;").println();
		}
		pw.println("	var idx = 0;");
		pw.println("	var page = 0;");
		pw.println("	this.suggestPages=[];");
		pw.println("	this.suggestHTML = '';");
		pw.println("	for(var a in this.htCityCode){");
		pw.println("		this.htCityName.push(a);");
		//pw.println("		var tag = this.htCityCode[a].split('|');");
		pw.println("		var tag = this.htCityCode[a];");
		//pw.println("		alert(tag['SUG']);");
		pw.println("		if(tag['SUG']==1){");
		pw.println("			page = idx/this.pagesize|0;");
		pw.println("			if(page > this.suggestPages.length){");
		pw.println("        		this.suggestPages.push(this.suggestHTML);");
		pw.println("				this.suggestHTML = '';");
		pw.println("			}");
		//pw.println("			this.suggestHTML +=	'<tr page='+ page +' style=\"dispaly:'+ (page > 0 ? 'none': '') +';\" onclick=\"jjcity.detailclick(this);\" onmouseover=\"jjcity.mouseover(this);\" onmouseout=\"jjcity.normal(this);\"><td>'+a + '</td><td style=\"text-align:right;\">' + tag[3]+ '</td></tr>';");
		pw.println("			this.suggestHTML +=	'<tr threecode=\"'+ tag['ID'] +'\" page='+ page +' onmouseover=\"jjcity.mouseover(this);\" onmouseout=\"jjcity.normal(this);\"  onmousedown=\"jjcity.detailclick(this);\" ><td>'+tag[this.leftCol] + '</td><td style=\"text-align:right;\">' + tag[this.rightCol]+ '</td></tr>';");

		pw.println("		}");
		pw.println("	}");
		pw.println("	if(this.suggestHTML){");
		pw.println("		this.suggestPages.push(this.suggestHTML);");
		pw.println("	}");
		pw.println("}");
		pw.println("var jjcity = new jjcitycode();");
		pw.println("jjcitycode.prototype.close= function(){");
		pw.println("	this.citycode.style.display = 'none';");
		pw.println("}");
		
		pw.println("jjcitycode.prototype.detailclick= function(obj){");
		pw.println("    var tagcol = jjcity.htCityCode[obj.threecode];");
		pw.println("	this.eventSrc.value =  tagcol[jjcity.rightCol];");
		pw.println("	this.eventSrcId.value = obj.threecode;");
		pw.println("	this.close();");
		pw.println("}");
		pw.println("jjcitycode.prototype.mouseover = function(obj){");
		pw.println("	obj.className = this.hotStyle;");
		pw.println("}");
		pw.println("jjcitycode.prototype.normal = function(obj){");
		pw.println("	obj.className = this.normailStyle;");
		pw.println("}");
		pw.println("jjcitycode.prototype.formatshow= function(){");
		pw.println(" 	var tagTable = $(this.matchData);");
		pw.println(		"if(!tagTable) return;");
		pw.println(" 	for(var iRow=0;iRow< tagTable.rows.length; iRow++) {");
		pw.println(" 		tagTable.rows[iRow].onclick = this.detailclick;");
		pw.println(" 		tagTable.rows[iRow].style.displsy = tagTable.rows[iRow] == this.currentpage ? '':'none';");
		pw.println(" 	}");
		pw.println("}");
		pw.println("jjcitycode.prototype.gotoPage= function(page,suggest){");
		pw.println(" 	var tagPages = this.pages;");
		pw.println("	this.currentpage = page;");
		pw.println("	if(suggest) tagPages=this.suggestPages;");
		pw.println("	if(tagPages.length <= page || page < 0){");
		pw.println("		return;");
		pw.println("	}");
		pw.println(" 	var tagTable = $(this.matchData+'div');");
		pw.println("    var tagHTML = '<table id=\"matchData\" cellSpacing=0 cellPadding=0 style=\"width:100%;\">';");
		pw.println(" 	tagHTML +=tagPages[page];");
		pw.println(" 	tagHTML +='</table>';");
		pw.println("	tagTable.innerHTML = tagHTML;");
		pw.println("}");
		pw.println("function getObjectById(id){ if(document.all) return(eval(\"document.all.\"+ id)); return(eval(id)); }");
		pw.println("function popuscitys() ");
		pw.println("{");
		pw.println("	var e = window.event.srcElement; ");
		pw.println("	jjcity.eventSrc = e;");
		pw.println("	jjcity.callBack = e.callBack;");
		pw.println("	popuscitysTag();");
		pw.println("}	");
		pw.println("function popuscitysTag()");
		pw.println("{");
		pw.println("	if(!jjcity.citycode)jjcity.citycode= getObjectById(\"jjcitycodechenwei\");");
		pw.println("	var e = window.event.srcElement;");
		pw.println("    writeCitysIframe();");
		pw.println("    var o = jjcity.citycode.style; jjcity.eventSrc = e;");
		pw.println("    var strname  = e.name;");
		pw.println("    jjcity.eventSrcId = $(strname.substr(0,strname.length-5));");
		pw.println("	if (arguments.length == 0) jjcity.objExport = e;");
		pw.println("    else jjcity.objExport = eval(arguments[0]);");
		pw.println("	var t = e.offsetTop,  h = e.clientHeight, l = e.offsetLeft, p = e.type;");
		pw.println("	while (e = e.offsetParent){t += e.offsetTop; l += e.offsetLeft;}");
		pw.println("    o.display = \"\"; ");
		pw.println("    var cw = jjcity.citycode.clientWidth, ch = jjcity.citycode.clientHeight;");
		pw.println("    var dw = document.body.clientWidth, dl = document.body.scrollLeft, dt = document.body.scrollTop;");
		pw.println("    ");
		pw.println("    if (document.body.clientHeight + dt - t - h >= ch) o.top = (p==\"image\")? t + h : t + h + 6;");
		pw.println("    else o.top  = (t - dt < ch) ? ((p==\"image\")? t + h : t + h + 6) : t - ch;");
		pw.println("    if (dw + dl - l >= cw) o.left = l; else o.left = (dw >= cw) ? dw - cw + dl : dl;");
		pw.println(" }");
		pw.println("");
		pw.println("function writeCitysIframe(){");
		pw.println("	jjcity.citycode.style.backgroundColor='white';");
		pw.println("	jjcity.citycode.style.border=\"1px solid #000000\" ");
		pw.println("}");
		pw.println("");
		/**
		* 建立输入控件对象；将
		* 一个input text控件变成由CtrlType指定的控件类型;
		* CtrlType:
		 "city":城市三字码输入控件
		 "airport":机场三字码输入控件
		 ...
		*/ 
		pw.println("function _MakeInputControl(oTextbox /*:HTMLInputElement*/,oTextboxid /*:HTMLInputElement*/,CtrlType){");
		pw.println(" switch(CtrlType.toLowerCase()){");
		pw.println("  case \"threecodecity\":");
		pw.println("   return new CAutoSuggest(oTextbox,oTextboxid,");
		pw.println("      new ISuggestionProvider(jjcity.htCityName,jjcity.htCityCode)); ");
		pw.println("  case \"threecodeairport\":");
		pw.println("   return new CAutoSuggest(oTextbox,oTextboxid,");
		pw.println("      new ISuggestionProvider(__htAirPortName,__htAirPortCode)); ");
		pw.println("  default:");
		pw.println("   return {};");
		pw.println(" }");
		pw.println("}");
		/*
		 提示接口定义：
		 如果要实现其它提示效果，要求实现getSuggestions方法
		 参数codeList是要输入的的代码的列表，Array;
		 参数codeMap是输入的代码映射名称的对象, function(JStype)==>HashMap(JavaType)
		*/
		pw.println("function ISuggestionProvider(codeList/*Array*/,codeMap/*HashMap*/) {");
		pw.println(" this._list=codeList;");
		pw.println(" this._listValue=codeMap;");
		pw.println("}");
		pw.println("ISuggestionProvider.prototype.getSuggestions = function (oCAutoSuggest /*:CAutoSuggest*/) {");
		pw.println("    var rtn = [];");
		pw.println("    var sTextboxValue = oCAutoSuggest.textbox.value.toUpperCase();");
		pw.println("    if (sTextboxValue && this._list.length > 0 ){");
        //查找匹配结果
		pw.println("        for (var i=0; i < this._list.length; i++) { ");
		pw.println("        	var tagcol = this._listValue[this._list[i]];");
		pw.println("        	if (tagcol[jjcity.leftCol].toUpperCase().indexOf(sTextboxValue) == 0 || tagcol[jjcity.rightCol].toUpperCase().indexOf(sTextboxValue)==0) {");
		pw.println("                rtn.push(tagcol['ID']);");
		pw.println("            } ");
		pw.println("        }");
		pw.println("    }");
		//pw.println("	alert(rtn.length);");
		pw.println("    oCAutoSuggest.showSuggest(rtn);");
		pw.println("};");
		pw.println(" ");
		 
		/**
		 * 自动提示的输入控件
		 * 
		 */
		pw.println("function CAutoSuggest(oTextbox /*:HTMLInputElement*/,oTextboxid /*:HTMLInputElement*/, oProvider /*:SuggestionProvider*/) {");
		pw.println("    this.provider = oProvider;");
		pw.println("    this.textbox = oTextbox;");
		pw.println("    this.textboxid = oTextboxid;");
		pw.println("    this.initialize();");
		pw.println("}");
		pw.println("CAutoSuggest.prototype.showSuggest = function (SuggestionsList /*:Array*/) {");
		pw.println("	var strShow = \"\";");
		pw.println("    if (SuggestionsList.length > 0) {");
		pw.println("    	var tagcol = jjcity.htCityCode[SuggestionsList[0]];");
		pw.println("        this.typeAString(tagcol[jjcity.leftCol]);");
		pw.println("        strShow ='<table cellSpacing=0 cellPadding=0 style=\"width:100%;\"><tr><td>'+tagcol[jjcity.leftCol] +'</td><td>'+tagcol[jjcity.rightCol] + ',</td></tr></table>';");
		pw.println("        strShow +=\"<hr/>\";");
		pw.println("        strShow +='<div id = \"matchDatadiv\">';");
		pw.println("    	strShow +='<table id=\"matchData\" cellSpacing=0 cellPadding=0 style=\"width:100%;\">'");
		pw.println("		var showHTML = '';");
		pw.println("		jjcity.pages=[];");
		pw.println("        for (var i=0; i < SuggestionsList.length; i++) { ");
		pw.println("			page = i/jjcity.pagesize|0;");
		pw.println("			if(page > jjcity.pages.length){");
		pw.println("        		jjcity.pages.push(showHTML);");
		pw.println("				showHTML = '';");
		pw.println("			}");
		pw.println("        	tagcol = jjcity.htCityCode[SuggestionsList[i]]");
    	//三子代码|拼音或英文|拼音首字母或英文缩写|中文名称|是否推荐|国内国外区分 ID style="display:none;"
    	//0          1          2                    3       4       5        6 
		pw.println("        	showHTML +='<tr threecode=\"'+ tagcol['ID'] +'\" page=\"'+ page +'\" onmousedown=\"jjcity.detailclick(this);\" onmouseover=\"jjcity.mouseover(this);\" onmouseout=\"jjcity.normal(this);\"><td>'+tagcol[jjcity.leftCol] + '</td><td style=\"text-align:right;\">' + tagcol[jjcity.rightCol]+ '</td></tr>';");
		pw.println("            jjcity.pageCount = page +1;");
		pw.println("        } ");
		pw.println("        if(showHTML) {");
		pw.println("        	jjcity.pages.push(showHTML);");
		pw.println("        }");
		pw.println("    }");
		pw.println("    else{");
		pw.println("    	strShow +='没有找到数据。<hr/>';");
		pw.println("    	strShow +='<table id=\"matchData\" cellSpacing=0 cellPadding=0 style=\"width:100%;\">'");
		pw.println("    	jjcity.pages = jjcity.suggestPages;");
		pw.println("    }");
		pw.println("    strShow+= jjcity.pages[0];");
		pw.println("    strShow +='</table>';");
		pw.println("    strShow +='</div>';");
		//分页处理
		pw.println("    jjcity.currentpage = 0");
		pw.println("    if(jjcity.pageCount > 1){");
		pw.println("        strShow +='<table cellSpacing=0 cellPadding=0 style=\"width:100%;text-align:center;\"><tr><td style=\"text-align:center;\" ><span calss = \"'+jjcity.pageStyle + '\" onclick = \"jjcity.gotoPage(jjcity.currentpage-1);\">&nbsp;&lt;</span>'");
		pw.println("		var ipageStart = jjcity.currentpage -2 > 0 ? jjcity.currentpage -2:0;");
		pw.println("		var ipageEnd = ipageStart + 5;");
		pw.println("		for(var ipage =ipageStart;ipage < ipageEnd && ipage < jjcity.pageCount-1;ipage++){");
		pw.println("			if(jjcity.currentpage == ipage){");
		pw.println("				strShow +='<span id=\"jjcity_page_'+ ipage +'\" calss = \"'+jjcity.selectPageStyle + '\">&nbsp;'+(ipage +1)+'</span>';");
		pw.println("			}else{");
		pw.println("        		strShow +='<span id=\"jjcity_page_'+ ipage +'\" onclick=\"jjcity.gotoPage('+ ipage +')\" calss = \"'+jjcity.pageStyle + '\">&nbsp;'+(ipage +1)+'</span>';");
		pw.println("			}");
		pw.println("		}");
		pw.println("        strShow +='<span title= calss = \"'+jjcity.pageStyle + '\" onclick = \"jjcity.gotoPage(jjcity.currentpage+1);\">&nbsp;&gt;</span></td></tr></table>'");
		pw.println("    }");
		pw.println("    strShow +='</table>';");
		pw.println("    jjcity.citycode.innerHTML = strShow;");
		pw.println("    popuscitysTag();");
		pw.println("};");
		pw.println("CAutoSuggest.prototype.setShowName=function (value /*:string*/) {");
		pw.println("    var msg=this.msgSpan;");
		pw.println("    if(value!=null)");
		pw.println("     this.textbox.value=value.toUpperCase();");
		pw.println("    if(this.textbox.value.length==50){");
        //输入完全后提示： 
		pw.println("        var rtn=this.getCityBameByCode(this.textbox.value);");
		pw.println("        if(rtn==\"null\" || rtn==null || typeof(rtn)==\"undefined\"){");
		pw.println("         msg.style.display='';");
		pw.println("         msg.style.color='red';");
		pw.println("         msg.innerHTML=\"不存在!\"");
		pw.println("         msg.style.backgroundColor=\"\";");
		pw.println("        }else{");
		pw.println("         msg.style.display='';");
		pw.println("         msg.style.color='blue';");
		pw.println("         msg.innerHTML=rtn");
		pw.println("         msg.style.backgroundColor=\"infobackground\";");
		pw.println("        }");
		pw.println(" }");
		pw.println("}");
		pw.println("function getCitysSelectedText() {");
		pw.println("	if (window.getSelection) { ");
		pw.println("		// This technique is the most likely to be standardized");
		pw.println("		// getSelection() returns a Selection object, which we do not document");
		pw.println("		return window.getSelection().toString();");
		pw.println("	} ");
		pw.println("	else if (document.getSelection) { ");
		pw.println("		// This is an older, simpler technique that returns a string");
		pw.println("		return document.getSelection();");
		pw.println("	}");
		pw.println("	else if (document.selection) { ");
		pw.println("		// This is the IE-specific technique");
		pw.println("		// We do not document the IE selection property or TextRange objects");
		pw.println("		return document.selection.createRange().text;");
		pw.println("	}");
		pw.println("} ");
		
		/*根据城市三字代码获得城市名称*/
		pw.println("CAutoSuggest.prototype.getCityBameByCode=function (code){");
		pw.println(" return this.provider._listValue[code];");
		pw.println("}");
		pw.println("CAutoSuggest.prototype.processKeyUp = function (oEvent /*:Event*/) {");
		pw.println("    var iKeyCode = oEvent.keyCode;");
		//pw.println("    this.textbox.value=this.textbox.value.toUpperCase();");
		pw.println("    //window.status=this.textbox.value");
		pw.println("    //ignore non-character keys");
		pw.println("	if(iKeyCode==27){");
		pw.println("		jjcity.close();");
		//pw.println("    if ((iKeyCode < 32 && iKeyCode!=8) || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {");
		pw.println("    }else if ((iKeyCode < 32 && iKeyCode!=8) || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {");
		pw.println("    } else {");
		pw.println("        this.provider.getSuggestions(this);");
		pw.println("    }");
		pw.println("    if(typeof(this.textbox.iifShowError)==\"undefined\" || this.textbox.iifShowError==null){");
		pw.println("     if(this.textbox.value){");
		pw.println("      var rtn=this.getCityBameByCode(this.textbox.value);");
		pw.println("         if(rtn==\"null\" || rtn==null || typeof(rtn)==\"undefined\"){");
		pw.println("          this.textbox.style.color='red';");
		pw.println("         }else{");
		pw.println("          this.textbox.style.color='black';");
		pw.println("  }");
		pw.println(" }else{");
		pw.println("      this.textbox.style.color='red';");
		pw.println(" }");
		pw.println(" return;");
		pw.println("    }");
		pw.println("    if(this.textbox.iifShowError.toUpperCase()==\"NO\") return;");
		pw.println("    ");
		pw.println("    var msg=this.msgSpan;");
		pw.println("    //window.status=this.textbox.value");
		pw.println("    if(this.textbox.value.length==50){");
		/*根据城市三字代码获得城市名称*/
		pw.println("        var rtn=this.getCityBameByCode(this.textbox.value);");
		pw.println("        if(rtn==\"null\" || rtn==null || typeof(rtn)==\"undefined\"){");
		pw.println("         msg.style.display='';");
		pw.println("         msg.style.color='red';");
		pw.println("         msg.innerHTML=\"不存在!\"");
		pw.println("         msg.style.backgroundColor=\"\";");
		pw.println("        }else{");
		pw.println("         msg.style.display='';");
		pw.println("         msg.style.color='blue';");
		pw.println("         msg.innerHTML=rtn");
		pw.println("         msg.style.backgroundColor=\"infobackground\";");
		pw.println(" }");
		pw.println("    }else{");
		pw.println("        msg.style.display='none';");
		pw.println("    }");
		pw.println("};");
		pw.println("var _CAutoSuggestlKey=function(){};");
		pw.println("_CAutoSuggestlKey.keys=0;");
		pw.println("_CAutoSuggestlKey.getKeys=function(){");
		pw.println(" return (_CAutoSuggestlKey.keys++);");
		pw.println("}");
		pw.println("CAutoSuggest.prototype.initialize  = function () {");
		pw.println("    var oThis = this;");
		pw.println("    this.textbox.onkeyup = function (oEvent) {");
		pw.println("        if (!oEvent) {");
		pw.println("            oEvent = window.event;");
		pw.println("        }    ");
		pw.println("        oThis.processKeyUp(oEvent);");
		pw.println("    };");
		pw.println("    this.textbox.onkeydown = function (oEvent) {");
		pw.println("        if (!oEvent) {");
		pw.println("            oEvent = window.event;");
		pw.println("        }");
		pw.println("   	 	var iKeyCode = oEvent.keyCode;");
		pw.println("		var objValue = jjcity.eventSrc.value;");
		pw.println("		if(iKeyCode == 8){");
		pw.println("			if(objValue){");
		pw.println("				var seltext = getCitysSelectedText();");
		pw.println("    			if(!this.textbox){");
		pw.println("    				this.textbox = jjcity.eventSrc;");
		pw.println("    			}");
		pw.println("				if(seltext){");
		pw.println("    				var iStart = objValue.length - seltext.length - 1;");
		pw.println("					var iLength =  objValue.length;");
		pw.println("    				if (this.textbox.createTextRange) {//IE,用Range对象");
		pw.println("       				 	var oRange = this.textbox.createTextRange(); ");
		pw.println("        				oRange.moveStart(\"character\", iStart); ");
		pw.println("       					oRange.moveEnd(\"character\", iLength - iStart); ");
		pw.println("        				oRange.select();");
		pw.println("    				} else if (this.textbox.setSelectionRange) {////Mozilla,用setSelectionRange()方法");
		pw.println("        				this.textbox.setSelectionRange(iStart, iLength);");
		pw.println("    				}     ");
		pw.println("    				this.textbox.focus();      ");
		pw.println("				}");
		pw.println("			}");
		pw.println("		}");
		pw.println("    };");
		pw.println("    this.textbox.onfocus = function (oEvent) {");
		pw.println("        if (!oEvent) {");
		pw.println("            oEvent = window.event;");
		pw.println("        }    ");
		pw.println("        jjcity.eventSrc = oEvent.srcElement;");
		pw.println("        var strName = jjcity.eventSrc.name;");
		pw.println("        strName = strName.substr(0,strName.length-5) ;");
		pw.println("        jjcity.eventSrcId = $( strName )");
		pw.println("		var tagcol = jjcity.htCityCode[jjcity.eventSrcId.value];");
		pw.println("		if(tagcol){");
		pw.println("			jjcity.eventSrc.value = tagcol[jjcity.rightCol];");
		pw.println("        	var oRange = jjcity.eventSrc.createTextRange(); ");
		pw.println("        	oRange.select();");
		pw.println("		}");
		pw.println("	}");
		pw.println("    this.textbox.onblur = function (oEvent) {");
		pw.println("        if (!oEvent) {");
		pw.println("            oEvent = window.event;");
		pw.println("        }    ");
		pw.println("		var tagvalue = jjcity.eventSrc.value.toUpperCase();");
		pw.println("        if(tagvalue && tagvalue != ''){   ");
        //查找匹配结果
		pw.println("        	for(var a in jjcity.htCityCode){");
		pw.println("				var tagcol = jjcity.htCityCode[a];");
		pw.println("        		if (tagcol['ID'].toUpperCase() == tagvalue || tagcol[jjcity.leftCol].toUpperCase() == tagvalue || tagcol[jjcity.rightCol].toUpperCase() == tagvalue) {");
		//pw.println("					jjcity.eventSrc.value = tagcol['ID']+' '+tagcol[jjcity.leftCol]+' '+ tagcol[jjcity.rightCol];");
		pw.println("					jjcity.eventSrc.value = tagcol[jjcity.rightCol];");
		pw.println("					jjcity.eventSrcId.value = tagcol['ID'];");
		pw.println("					break;");
		pw.println("            	} ");
		pw.println("            } ");
		pw.println("        }");
		pw.println("        jjcity.citycode.style.display='none';    ");
		pw.println("        if(jjcity.eventSrc.callBack){");
		pw.println("			eval(jjcity.eventSrc.callBack +\"('\"+ jjcity.eventSrcId.value + \"','\"+jjcity.eventSrc.value+\"');\");");
		pw.println("        }");
		pw.println("    };");
		pw.println("    this.textbox.ondblclick = function (oEvent) {");
		pw.println("        if (!oEvent) {");
		pw.println("            oEvent = window.event;");
		pw.println("        }");
		pw.println("	    popuscitysTag();");
		pw.println("    };");
	    //三字码输入长度限制
		pw.println("    this.textbox.maxLength=50;    ");
		pw.println("    //this.textbox.style.width=\"100px\";");
	    //加入自动提示标签
		pw.println("    var i=_CAutoSuggestlKey.getKeys();");
		pw.println("    this.textbox.insertAdjacentHTML(\"afterEnd\",\"<span style='display:none;font-size:12px;color:red;'\"+");
		pw.println("     \" id='_autosugTextbox_\"+i+\"' ></span>\");");
		pw.println("    this.msgSpan=document.getElementById(\"_autosugTextbox_\"+i);");
		pw.println("   // popuscitysTag();");
		pw.println("};");
		/**
		 * TextBox的辅助方法；选择textbox里面起始的Range对象
		 */
		pw.println("CAutoSuggest.prototype.selectRange = function (iStart /*:int*/, iLength /*:int*/) {");
		pw.println("    if (this.textbox.createTextRange) {//IE,用Range对象");
		pw.println("        var oRange = this.textbox.createTextRange(); ");
		pw.println("        oRange.moveStart(\"character\", iStart); ");
		pw.println("        oRange.moveEnd(\"character\", iLength - this.textbox.value.length);      ");
		pw.println("        oRange.select();");
		pw.println("    } else if (this.textbox.setSelectionRange) {////Mozilla,用setSelectionRange()方法");
		pw.println("        this.textbox.setSelectionRange(iStart, iLength);");
		pw.println("    }     ");
		pw.println("    this.textbox.focus();      ");
		pw.println("}; ");
		/**
		 * TextBox的辅助方法；为textbox后面加入自动输入的字符串，并且highlight;
		 * 
		 */
		pw.println("CAutoSuggest.prototype.typeAString = function (str) {");
		pw.println("    if (this.textbox.createTextRange || this.textbox.setSelectionRange){");
		pw.println("        var iLen = this.textbox.value.length; ");
		pw.println("        this.textbox.value = str; ");
		pw.println("        this.selectRange(iLen, str.length);");
		pw.println("    }");
		pw.println("};");
		pw.println("function SetupAllControls(){");
		pw.println(" var inputs=document.getElementsByTagName(\"INPUT\");");
		pw.println(" for(var i=0;i<inputs.length;i++){");
		pw.println("  var obj=inputs[i];");
		pw.println("  if(obj.tagName!=null && typeof(obj.tagName)!=\"undefined\"){");
		pw.println("   if(obj.tagName==\"INPUT\"){");
		pw.println("    if(obj.type==\"text\" && (obj.CtrlType!=null && typeof(obj.CtrlType)!=\"undefined\")){");
		pw.println("     var objid = $(obj.tagid);");
		pw.println("     obj.CAutoSugObject=_MakeInputControl(obj,objid,obj.CtrlType);");
		pw.println("     obj.CAutoSugObject.setShowName(obj.value);");
		pw.println("    }");
		pw.println("   }");
		pw.println("  }");
		pw.println(" }");
		pw.println("}");
		pw.println("function __SecuritySetup(){");
		pw.println(" if (document.readyState != 'complete') {");
		pw.println("  window.setTimeout(SetupAllControls,150);");
		pw.println(" }else{");
		pw.println("  window.setTimeout(SetupAllControls,50);");
		pw.println(" }");
		pw.println("}");
		pw.println("if ( window.onload != null ) {");
		pw.println(" var __oldOnload = window.onload;");
		pw.println(" window.onload = function ( e ) {");
		pw.println("  __oldOnload( e );");
		pw.println("  __SecuritySetup();");
		pw.println(" };");
		pw.println("}");
		pw.println("else{");
		pw.println(" __SecuritySetup();");
		pw.println("}");
		return SUCCESS;
	}
	
//	@SuppressWarnings("unchecked")
//	public List<Map> findCitys(String str){
//		//获取application对象
//		ActionContext context =ServletActionContext.getContext();
//		Map map =  context.getApplication();
//		String key = SessionConst.CITYS + str;
//		List<Map> mapAction = (List<Map>)map.get(key);
//		if(mapAction == null){
//			mapAction = citysService.findCitys(str);
//			map.put(key, mapAction);
//		}
//		return mapAction;
//	}


	//pw.println("    //window.status=this.textbox.value");
	

	
}
